﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Obligatorio1.Modelo;
using Obligatorio1.Controladores;

namespace Obligatorio1
{
    class ManejadorEmail
    {
        private static ManejadorEmail manejador = null;

        public static ManejadorEmail getInstance()
        {
            if (manejador == null)
            { manejador = new ManejadorEmail(); }
            return manejador;
        }

        public void agregarEmail(Email e)
        {
            try
            {            
                Obligatorio1Entities oe = GestorModelo.getInstance().getModelo();
                oe.Email.AddObject(e);
                oe.SaveChanges();
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
        }

        public void borrarEmail(Email e)
        {
            try
            {            
                Obligatorio1Entities oe = GestorModelo.getInstance().getModelo();
                oe.Email.DeleteObject(e);
                oe.SaveChanges();
            }
            catch (Exception ex) { Console.WriteLine(ex.Message); }
        }

        public List<Email> listar() 
        {
            Obligatorio1Entities oe = GestorModelo.getInstance().getModelo();
            var ems = from e in oe.Email
                           select e;
            return ems.ToList(); 
        }

        public String exportar(char separador, int idPersona)
        {
            String exp = "";

            Obligatorio1Entities oe = GestorModelo.getInstance().getModelo();

            var emailPersona = from Email d in oe.Email.DefaultIfEmpty()
                                     where (d.idPersona == idPersona)
                                     select d;

            foreach (var e in emailPersona)
            {
                exp += separador + e.email + separador + e.tipo;
            }

            return exp;
        }

        internal bool Igual(Email email, Email e)
        {
            return email.email.Equals(e.email) && email.idPersona == e.idPersona;
        }
    }       
}
